package com.ajie.ft_kvdata.sp;

import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import static android.os.ParcelFileDescriptor.MODE_WORLD_READABLE;
import static android.os.ParcelFileDescriptor.MODE_WORLD_WRITEABLE;
import static com.ajie.ft_kvdata.BuildConfig.DEBUG;

/**
 * Created by zhangyingjie on 2020/10/17
 *
 * @function
 */
public class MContextImpl {
    private static final String TAG = "MContextImpl";
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressWarnings("deprecation")
    static void setFilePermissionsFromMode(String name, int mode,
                                           int extraPermissions) {
        int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
                |FileUtils.S_IRGRP|FileUtils.S_IWGRP
                |extraPermissions;
        if ((mode&MODE_WORLD_READABLE) != 0) {
            perms |= FileUtils.S_IROTH;
        }
        if ((mode&MODE_WORLD_WRITEABLE) != 0) {
            perms |= FileUtils.S_IWOTH;
        }
        if (DEBUG) {
            Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
                    + ", perms=0x" + Integer.toHexString(perms));
        }
        FileUtils.setPermissions(name, perms, -1, -1);
    }
}
